#ifndef AABB_H
#define AABB_H
#include "interval.h"
#include "Ray.h"
#include "Vec3.h"

class AABB {
public:
    Interval x, y, z;

    AABB() = default; // The default AABB is empty, since intervals are empty by default.

    AABB(const AABB&, const AABB&);

    AABB(const Interval& x, const Interval& y, const Interval& z);

    AABB(const Point3& a, const Point3& b) {
        // Treat the two points a and b as extrema for the bounding box, so we don't require a
        // particular minimum/maximum coordinate order.

        x = (a[0] <= b[0]) ? Interval(a[0], b[0]) : Interval(b[0], a[0]);
        y = (a[1] <= b[1]) ? Interval(a[1], b[1]) : Interval(b[1], a[1]);
        z = (a[2] <= b[2]) ? Interval(a[2], b[2]) : Interval(b[2], a[2]);
    }

    const Interval& axis_interval(int n) const {
        if (n == 1) return y;
        if (n == 2) return z;
        return x;
    }

    bool hit(const Ray& r, Interval ray_t) const;

private:
    void pad_to_minimums();
};

AABB operator+(const AABB& bbox, const Vec3& offset);
AABB operator+(const Vec3& offset, const AABB& bbox);

#endif
